/**
 * @Title: SecurityFilter.java
 * @Package com.qding.park.api.web.service.filter
 * @Description: TODO
 * Copyright: Copyright (c) 2011 
 * Company:千丁互联
 * 
 * @author Comsys-Administrator
 * @date 2016年8月16日 下午4:28:50
 * @version V1.0
 */

package com.qding.park.manager.rest.filter;

import java.io.IOException;
import java.util.Collection;
import java.util.Map;

import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;

import org.apache.catalina.filters.AddDefaultCharsetFilter.ResponseWrapper;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.qding.park.common.web.enums.HttpStatusEnum;
import com.qding.park.common.web.filter.BaseFilter;
import com.qding.park.common.web.util.WebUtils;
import com.qding.park.common.web.util.WrapperedResponse;

/**
 * @ClassName: SecurityFilter
 * @Description: TODO
 * @author Comsys-Administrator
 * @date 2016年8月16日 下午4:28:50 此过滤器要设置一个例外
 */
@WebFilter(filterName = "securityCommonFilter", urlPatterns = "/*", initParams = {
		@WebInitParam(name = "EXCLUDED_PAGES", value = "createToken.do;createToken.jhtml;createToken;parking/calRules") })
// @WebFilter(filterName="myFilter",urlPatterns="/*")
public class SecurityCommonFilter extends BaseFilter {

	private static final Log _log = LogFactory.getLog(SecurityCommonFilter.class);
	/**
	 * @Fields excludedPages : 例外页面（用一句话描述这个变量表示什么）
	 */
	private String excludedPages;

	/**
	 * @Fields excludedPageArray : 例外页面数组（用一句话描述这个变量表示什么）
	 */
	private String[] excludedPageArray;

	/*
	 * <p>Title: destroy</p> <p>Description: </p>
	 * 
	 * @see javax.servlet.Filter#destroy()
	 */
	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		System.out.println("过滤器销毁");
	}

	/*
	 * <p>Title: doFilter</p> <p>Description: </p>
	 * 
	 * @param arg0
	 * 
	 * @param arg1
	 * 
	 * @param arg2
	 * 
	 * @throws IOException
	 * 
	 * @throws ServletException
	 * 
	 * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest,
	 * javax.servlet.ServletResponse, javax.servlet.FilterChain)
	 */
	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		HttpServletRequest req = (HttpServletRequest) request;
		HttpServletResponse resp = (HttpServletResponse) response;
		boolean isExcludedPage = false;

		if (excludedPageArray != null) {

			for (String page : excludedPageArray) {// 遍历例外url数组

				// 判断当前URL是否与例外页面相同
				if (req.getServletPath().substring(1).equals(page)) { // 从第2个字符开始取（把前面的/去掉）
					// System.out.println(page + ", you're excluded.");
					_log.info(req.getServletPath().substring(1) + " excluded");
					isExcludedPage = true;
					break;
				}
			}
		}
//		WrapperedResponse wrapResponse = new WrapperedResponse((HttpServletResponse) response);
		if (isExcludedPage) {
			_log.info(req.getServletPath() + " 过滤不处理!!!");
			chain.doFilter(request, response);
		} else {
			Map<String, Object> paraMap = WebUtils.buildParamGp(req);
			_log.info(req.getServletPath() + " params=" + paraMap + " 经过了过滤!!");
//			chain.doFilter(request, wrapResponse);
			chain.doFilter(request, response);

			int status = resp.getStatus();
			_log.info("response　返回:: httpstatus=" + status);
//			if (HttpStatusEnum.C_200.getCode() == status) {
////				_log.info("");
//				// 成功返回
////				byte[] data = wrapResponse.getResponseData();
////				String tempOrg = new String(data, "utf-8");
////				_log.info("response　返回:: httpstatus=" + status + " data=" + tempOrg);
////				ServletOutputStream out = response.getOutputStream();
////				out.write(data);
////				out.flush();
//			} else {
//				_log.info("response　返回:: httpstatus=" + status);
//			}

		}

	}

	/*
	 * <p>Title: init</p> <p>Description: </p>
	 * 
	 * @param arg0
	 * 
	 * @throws ServletException
	 * 
	 * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
	 */
	@Override
	public void init(FilterConfig fConfig) throws ServletException {
		// TODO Auto-generated method stub
		System.out.println("过滤器初始化");
		excludedPages = fConfig.getInitParameter("EXCLUDED_PAGES");
		if (null != excludedPages && excludedPages.length() != 0) { // 例外页面不为空
			excludedPageArray = excludedPages.split(String.valueOf(';'));
		}
	}

}
